package com.zuipin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.BaseRegion;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IBaseRegionService;
import com.zuipin.util.RegexUtils;
import com.zuipin.vo.BaseRegionVo;

/**
 * 基础资料-地区Controller
 * 
 * @ClassName: BaseRegionController
 * @Description: TODO
 * @author robot
 * @date 2017年04月257日 08:30:25
 */
@Api(value = "地区-chenjunjie", description = "地区-chenjunjie")
@RestController
@RequestMapping(value = "/baseRegion")
public class  BaseRegionController {
	@Resource
	public IBaseRegionService baseRegionService;

	
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	@ApiOperation(value = "根据主键查询基础资料-地区", notes = "查询基础资料-地区")
	public Result<BaseRegion> getBaseRegionById(@ApiParam(required = true, name = "id", value = "主键,序列自增") @RequestParam Integer id){
		BaseRegion baseRegion = baseRegionService.findById(id);
		Result<BaseRegion> result = new Result<BaseRegion>();
		if (baseRegion != null) {
			result.setData(baseRegion);
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setErrorCode(ErrorCode.ELSE);
		}
		return result;
	}
	
	
	@RequestMapping(value = "/getByCode", method = RequestMethod.GET)
	@ApiOperation(value = "根据地区编码查询地区-chenjunjie", notes = "根据地区编码查询地区-chenjunjie")
	@Valids({ @Valid(name = "code", minLength = 6,maxLength=6,regex=RegexUtils.CODE) })
	@ApiImplicitParams({
			@ApiImplicitParam(name = "code", value = "地区编码", required = true,  paramType = "query")
	})
	public Result<BaseRegion> getRegionByCode(@ApiParam(required = true, name = "code", value = "地区编码") @RequestParam String code){
		BaseRegion baseRegion = baseRegionService.findByCode(code);
		Result<BaseRegion> result = new Result<BaseRegion>();
		if (baseRegion != null) {
			result.setData(baseRegion);
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setErrorCode(ErrorCode.ELSE);
		}
		return result;
	}
	
	
	@RequestMapping(value = "/getByParentCode", method = RequestMethod.GET)
	@ApiOperation(value = "根据父编码查询地区-chenjunjie", notes = "根据父编码查询地区-chenjunjie")
	@Valids({ @Valid(name = "parentCode", minLength = 6,maxLength=6,regex=RegexUtils.CODE) })
	@ApiImplicitParams({
			@ApiImplicitParam(name = "parentCode", value = "地区父编码", required = true,  paramType = "query")
	})
	public Result<List<BaseRegionVo>> getByParentCode(@ApiParam(required = true, name = "parentCode", value = "地区父编码") @RequestParam String parentCode){
		List<BaseRegionVo> regions = baseRegionService.findByParentCode(parentCode);
		PageResult<List<BaseRegionVo>> result = new PageResult<List<BaseRegionVo>>();
		if(regions==null||regions.size()==0){
			result.setData(null);
			result.setTotal_record(0);
			result.setErrorCode(ErrorCode.NOT_FIND);
			return result;
		}
		result.setData(regions);
		result.setTotal_record(regions.size());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	
	@RequestMapping(value = "/getByLevel", method = RequestMethod.GET)
	@ApiOperation(value = "根据城市级别查询地区-chenjunjie", notes = "根据城市级别查询地区-chenjunjie")
	@Valids({ @Valid(name = "regionLevel", minValue = 1,maxValue=3) })
	@ApiImplicitParams({
			@ApiImplicitParam(name = "regionLevel", value = "地区级别 1:省份/自治区/直辖市,2:城市,3:县/区", required = true,  paramType = "query")
	})
	public Result<List<BaseRegion>> getByLevel(@ApiParam(required = true, name = "regionLevel", value = "地区级别 1:省份/自治区/直辖市,2:城市,3:县/区") @RequestParam Integer regionLevel){
		List<BaseRegion> regions = baseRegionService.findByLevel(regionLevel);
		PageResult<List<BaseRegion>> result = new PageResult<List<BaseRegion>>();
		if(regions==null||regions.size()==0){
			result.setData(null);
			result.setTotal_record(0);
			result.setErrorCode(ErrorCode.NOT_FIND);
			return result;
		}
		result.setData(regions);
		result.setTotal_record(regions.size());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/getRegionTree", method = RequestMethod.GET)
	@ApiOperation(value = "查询地区(树形结构)-chenjunjie", notes = "查询地区(树形结构)-chenjunjie")
	public Result<List<BaseRegionVo>> getRegionTree(){
		List<BaseRegionVo> regions = baseRegionService.findRegionTree(BaseRegion.LEVEL_PROVICE);
		PageResult<List<BaseRegionVo>> result = new PageResult<List<BaseRegionVo>>();
		if(regions==null||regions.size()==0){
			result.setData(null);
			result.setTotal_record(0);
			result.setErrorCode(ErrorCode.NOT_FIND);
			return result;
		}
		result.setData(regions);
		result.setTotal_record(regions.size());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	@ApiOperation(value = "查询全部地区-chenjunjie", notes = "查询全部地区-chenjunjie")
	@Valids({ @Valid(name = "code", minLength = 6,maxLength=6) })
	public Result<List<BaseRegion>> getAll(){
		List<BaseRegion> regions = baseRegionService.findAll();
		PageResult<List<BaseRegion>> result = new PageResult<List<BaseRegion>>();
		if(regions==null||regions.size()==0){
			result.setData(null);
			result.setTotal_record(0);
			result.setErrorCode(ErrorCode.NOT_FIND);
			return result;
		}
		result.setData(regions);
		result.setTotal_record(regions.size());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	
}
